package davidlauzon.activerecord.nodes;


import davidlauzon.activerecord.nodes.Node;
import davidlauzon.activerecord.nodes.field.Field;
import davidlauzon.activerecord.visitor.SqlSerializer;

public class Column extends Node implements Cloneable, AliasableColumnIF, SortableColumnIF
{
    /*********************************************************************************************
     * VARIABLES
     *********************************************************************************************/
    private Field  _source;
    private String _alias;
    
    private SortDirection _sort;
    
    
    
    /*********************************************************************************************
     * SIMPLE GETTERS & SETTERS
     *********************************************************************************************/
    public boolean isPrimaryKey() { return _source.isPrimaryKey(); }

    @Override public SortDirection sortDirection() { return _sort; }
    @Override public boolean hasSortDirection()    { return _sort != null; } 
    @Override public boolean isAlias()  { return _alias != null; }
    @Override public String  alias()    { return _alias; }
    @Override public String  name()     { return _source.name(); }
              public Field   source()   { return _source; }
    
    
    /*********************************************************************************************
     * PUBLIC METHODS
     *********************************************************************************************/
    public Column(Field source, String alias, SortDirection sort) {
        _source = source;
        _alias  = alias;
        _sort   = sort;
    }
    
    
    public Column as(String alias) {
        Column other = null;
        try {
            other = (Column) clone();
            other._alias = alias;
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return other;
    }
    
    public Column asc() {
        Column other = null;
        try {
            other = (Column) clone();
            other._sort = SortDirection.ASC;
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return other;
    }
    
    public Column desc() {
        Column other = null;
        try {
            other = (Column) clone();
            other._sort = SortDirection.DESC;
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return other;
    }
    
    @Override
    public String accept(SqlSerializer visitor) {
        return visitor.visit( this );
    }
    
    public String toString() {
        if (hasSortDirection()) {
            if (isAlias()) {
                return alias() + " " + sortDirection();
            } else {
                return name() + " " + sortDirection();
            }
        } else if (isAlias()) {
            return name() + " AS " + alias();
        } else {
            return name();
        }
    }
}
